using System;
using SMS.SMPPClient.PDU;

namespace SMS.SMPPClient.PDU.Incoming
{
    /// <summary>
    /// Sent from the SMSC to the mobile device when the device is available and a
    /// delivery pending flag has been set from a previous data_sm operation.
    /// </summary>
    public class SMPPAlertNotification : SMPPResponse
    {
        private byte _SourceAddrTON;
        private byte _SourceAddrNPI;
        private string _SourceAddress;
        private byte _ESMEAddrTON;
        private byte _ESMEAddrNPI;
        private string _ESMEAddress;

        /// <summary>
        /// Enumerates the availability states of the message.
        /// </summary>
        public enum AvailabilityStatusType :
            byte
        {
            Available = 0x00,
            Denied = 0x01,
            Unavailable = 0x02
        }

        /// <summary>
        /// Enumerates the type of number.
        /// </summary>
        public TONType SourceAddressTON
        {
            get
            {
                return (TONType)Enum.Parse(typeof(TONType),
                                                 Enum.GetName(typeof(TONType),
                                                             _SourceAddrTON));
            }
        }

        /// <summary>
        /// Enumerates the numbering plan indicator.
        /// </summary>
        public NPIType SourceAddressNPI
        {
            get
            {
                return (NPIType)Enum.Parse(typeof(NPIType),
                                                 Enum.GetName(typeof(NPIType),
                                                             _SourceAddrNPI));
            }
        }

        /// <summary>
        /// Address of sending entity.
        /// </summary>
        public string SourceAddress
        {
            get
            {
                return _SourceAddress;
            }
        }

        /// <summary>
        /// The type of number for the destination address that requested an alert.
        /// </summary>
        public TONType ESMEAddressTON
        {
            get
            {
                return (TONType)Enum.Parse(typeof(TONType),
                                                 Enum.GetName(typeof(TONType),
                                                             _ESMEAddrTON));
            }
        }

        /// <summary>
        /// The numbering plan indicator for the destination address that requested an alert.
        /// </summary>
        public NPIType ESMEAddressNPI
        {
            get
            {
                return (NPIType)Enum.Parse(typeof(NPIType),
                                                 Enum.GetName(typeof(NPIType),
                                                             _ESMEAddrNPI));
            }
        }

        /// <summary>
        /// The source address of the device that requested an alert.
        /// </summary>
        public string ESMEAddress
        {
            get
            {
                return _ESMEAddress;
            }
        }

        /// <summary>
        /// The status of the mobile station.
        /// </summary>
        public AvailabilityStatusType MSAvailabilityStatus
        {
            get
            {
                return (AvailabilityStatusType)Enum.Parse(typeof(AvailabilityStatusType),
                        Enum.GetName(typeof(AvailabilityStatusType),
                                        tlvTable.GetOptionalParamBytes(
                                            (ushort)PDU.OptionalParamCodes.ms_availability_status)[0]));
            }
        }

        /// <summary>
        /// Creates an SMPP Alert Notification PDU.
        /// </summary>
        public SMPPAlertNotification(byte[] response)
            : base(response)
        {
            DecodeSMSCResponse();
        }

        /// <summary>
        /// Decodes the alert_notification from the SMSC.
        /// </summary>
        protected override void DecodeSMSCResponse()
        {
            byte[] remainder = ResponseAfterHeader;
            _SourceAddrTON = remainder[0];
            _SourceAddrNPI = remainder[1];
            _SourceAddress = GetStringFromBody(ref remainder, 2);
            _ESMEAddrTON = remainder[0];
            _ESMEAddrNPI = remainder[1];
            _ESMEAddress = GetStringFromBody(ref remainder, 2);

            //fill the TLV table if applicable
            tlvTable.TranslateTLVDataIntoTable(remainder, 2);
        }
    }
}
